#!/bin/bash
set -e

echo -e "\e[32m[1/4] 安装依赖和Dante服务...\e[0m"
sudo apt update
sudo apt install -y dante-server

echo -e "\e[32m[2/4] 创建代理用户...\e[0m"
sudo useradd -r -s /usr/sbin/nologin 123
echo "123:123" | sudo chpasswd

echo -e "\e[32m[3/4] 配置Dante...\e[0m"
sudo bash -c 'cat > /etc/danted.conf <<EOF
internal: 0.0.0.0 port = 16585
external: eth0
method: username
user.privileged: root
user.notprivileged: nobody

client pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
}
pass {
    from: 0.0.0.0/0 to: 0.0.0.0/0
    command: bind connect udpassociate
    method: username
}
EOF'

echo -e "\e[32m[4/4] 启动服务...\e[0m"
sudo systemctl restart danted
sudo systemctl enable danted

# 验证安装
IP=$(curl --socks5 127.0.0.1:16585 -U 123:123 -s http://ifconfig.me || echo "失败")
if [[ "$IP" == "失败" ]]; then
    echo -e "\e[31m❌ 代理测试失败，请检查：sudo journalctl -u danted -b\e[0m"
else
    echo -e "\e[32m✅ 代理工作正常，出口IP: $IP\e[0m"
fi

echo -e "\n\e[1;36mSOCKS5代理配置完成！\e[0m"
echo -e "\e[33m用户名: 123  密码: 123\e[0m"
echo -e "\e[33m端口: 16585\e[0m"
echo -e "\e[33m修改密码: sudo passwd 123\e[0m"
echo -e "\e[33m更改端口: sudo nano /etc/danted.conf (修改port值后重启服务)\e[0m"
echo -e "\e[33m测试命令: curl --socks5 127.0.0.1:16585 -U 123:123 http://ifconfig.me\e[0m"